/**
 * Copyright (C) 2013 SHANGHAI VOLKSWAGEN, All rights reserved.
 * License version 1.0, a copy of which has been included with this.
 *
 * @File name：com.bwie.community.websocket.WebSocketUsers
 * @Create on：2024/4/5
 * @Author：liuchang
 */
package com.bwie.community.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>ClassName：WebSocketUsers</p >
 * <p>Description：</p >
 * <p>Author：liuchang</p >
 * <p>Date：2024/4/5</p >
 */
public class WebSocketUsers {
    /*
    * 日志控制器
    * */
    private static final Logger LOGGER= LoggerFactory.getLogger(WebSocketUsers.class);

    /*
    * 用户集
    * */
    private static Map<String, Session> USERS=new ConcurrentHashMap<String,Session>();

    /*
    * 存储用户
    * */
    public static void put(String key,Session session){
       USERS.put(key,session);
    }
    /*
    *移除用户
    * */
    public static boolean remove(Session session){
        String key=null;
        boolean flag=USERS.containsKey(session);

        if (flag){

        }else {
            return true;
        }
           return remove(key);
    }

    public static boolean remove(String key)
    {
        LOGGER.info("\n 正在移出用户 - {}", key);
        Session remove = USERS.remove(key);
        if (remove != null)
        {
            boolean containsValue = USERS.containsValue(remove);
            LOGGER.info("\n 移出结果 - {}", containsValue ? "失败" : "成功");
            return containsValue;
        }
        else
        {
            return true;
        }
    }

    /**
     * 获取在线用户列表
     *
     * @return 返回用户集合
     */
    public static Map<String, Session> getUsers()
    {
        return USERS;
    }

    /**
     * 群发消息文本消息
     *
     * @param message 消息内容
     */
    public static void sendMessageToUsersByText(String message)
    {
        Collection<Session> values = USERS.values();
        for (Session value : values)
        {
            sendMessageToUserByText(value, message);
        }
    }


    /**
     * 发送文本消息
     *
     * @param userName 自己的用户名
     * @param message 消息内容
     */
    public static void sendMessageToUserByText(Session session, String message)
    {
        if (session != null)
        {
            try
            {
                session.getBasicRemote().sendText(message);
            }
            catch (IOException e)
            {
                LOGGER.error("\n[发送消息异常]", e);
            }
        }
        else
        {
            LOGGER.info("\n[你已离线]");
        }
    }
}
